package ewgenius.testgame.main.objects;

import java.util.Random;

import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.*;

import ewgenius.xube.XubeEngine;
import ewgenius.xube.engine.InputState;
import ewgenius.xube.engine.entities.Entity;
import ewgenius.xube.engine.graphics.Render;
import ewgenius.xube.engine.graphics.gui.*;

public class TestEntity1 extends Entity {

	private Vector2f position = new Vector2f();
	private Vector2f position1 = new Vector2f();
	private Vector2f direction = new Vector2f(1, 1);
	private Vector2f direction1 = new Vector2f(1, 1);
	private Color color = new Color(Color.WHITE);
	private Color color1 = new Color(Color.BLUE);
	private Color color2 = new Color(Color.RED);
	private Color color3 = new Color(Color.GREEN);
	private int speed = 1;
	private int size = 1;
	private int count = 100;

	private void updatePosition() {
		position.x += direction.x * speed;
		position.y += direction.y * speed;
	}

	Random random = new Random();

	public TestEntity1(String name, int id) {
		super(name, id);

		init();
	}

	float time = 0;

	public void init() {
		position.x = random.nextInt(800);
		position.y = random.nextInt(600);

		direction.x = (random.nextInt(100)+ 1) * random.nextInt(2) == 1 ? -1 : 1;
		direction.y = (random.nextInt(100)+ 1) * random.nextInt(2) == 1 ? -1 : 1;
		
		direction.normalise();

		// size = 1;

		// color = color1;
	}

	public void update(int time, XubeEngine engine) {
		// size = random.nextInt(50);
		InputState input = engine.getInputState();
		updatePosition();

		if (id > 0) {
			TestEntity1 p2 = (TestEntity1) engine.getEntities().get(id - 1);
			position1.x = p2.position.x;
			position1.y = p2.position.y;
		} else {
			TestEntity1 p2 = (TestEntity1) engine.getEntities().get(49999);
			position1.x = p2.position.x;
			position1.y = p2.position.y;
		}

		/*
		 * if ((position.x >= player.position.x - 10 && position.x <=
		 * player.position.x + 10) && (position.y >= player.position.y - 10 &&
		 * position.y <= player.position.y + 10)) { direction.x = 0; direction.y
		 * = 0; color = color3; size = 5; }
		 */

		if (input.keyF1)
			speed = 2;
		if (input.keyF2)
			speed = -2;
		if (input.keyF3)
			init();
		if (input.keyF4)
			speed = 0;

		direction1.x = -(position.x - input.mouseX);
		direction1.y = -(position.y - input.mouseY);
		if (direction1.length() > 0)
			direction1.normalise();
		direction.x = -(position.x - position1.x);
		direction.y = -(position.y - position1.y);
		if (direction.length() > 0)
			direction.normalise();
		if (input.mouseButton[0])
			direction.add(direction, direction1, direction);
		
		
		direction1.x = direction.x;
		direction1.y = direction.y;
		
		if (position.x >= 800 || position.x <= 0) {
			direction.x *= -1;
			color = color1;
		}
		if (position.y >= 600 || position.y <= 0) {
			direction.y *= -1;
			color = color2;
		}
		
		direction.add(direction, direction1, direction);
	}

	public void render(Render render) {
		render.begin2DRender();

		render.draw2DRect(position, size, size, color);

		render.end2DRender();
	}
}